package com.rzgeo.rzmaplib.comp.geoview;

import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.LayerList;
import com.esri.arcgisruntime.mapping.RasterElevationSource;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.GeoView;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.util.ListenableList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanghang on 2018/1/20.
 */

public class RmlSceneViewHolder implements IRmlGeoViewHolder {

    private final SceneView _sceneView;

    public RmlSceneViewHolder(SceneView sceneView){
        _sceneView=sceneView;
        _sceneView.setAttributionTextVisible(false);
    }

    @Override
    public ILocMgr getLocMgr() {
        return null;
    }

    @Override
    public void onMapTouch(OnTouchListener onTouchListener) {

    }

    @Override
    public void addGraphicsOverlay(GraphicsOverlay gls) {
        _sceneView.getGraphicsOverlays().add(gls);
//                final GraphicsOverlay gls2 = new GraphicsOverlay();
//                gls.getGraphics().addListChangedListener(new ListChangedListener<Graphic>() {
//                    @Override
//                    public void listChanged(ListChangedEvent<Graphic> e) {
//                        switch (e.getAction()){
//                            case ADDED:
//                                gls2.getGraphics().add(e.getIndex(),e.getItems());
//                                break;
//                            case REMOVED:
//                                gls2.getGraphics().removeAll(e.getItems());
//                                break;
//                        }
//
//                    }
//                });
//
//                _mapView.getGraphicsOverlays().add(gls2);
    }

    @Override
    public void setViewPoint(Point llPoint) {
        Camera camera = new Camera(llPoint, 4500, 0, 30, 0);
        _sceneView.setViewpointCameraAsync(camera);
    }

    @Override
    public void setViewExtent(Envelope ext) {

        Camera camera = new Camera(ext.getCenter(), 4500, 0, 30, 0);
        _sceneView.setViewpointCamera(camera);
    }

    @Override
    public void setBaseMap(ArcGISTiledLayer tlayer, final String elevationSourcePath) {

        ArcGISScene scene = new ArcGISScene(new Basemap(tlayer));
        if (elevationSourcePath != null) {
            ArrayList<String> fs = new ArrayList<String>() {{
                add(elevationSourcePath);
            }};
            scene.getBaseSurface().getElevationSources().add(new RasterElevationSource(fs));
        }
        _sceneView.setScene(scene);
    }

    @Override
    public void addDomLayer(ArrayList<Layer> layers) {
        _sceneView.getScene().getBasemap().getBaseLayers().addAll(layers);
    }

    @Override
    public void addDemRasterLayer(ArrayList<Layer> demLayers) {
        _sceneView.getScene().getOperationalLayers().addAll(demLayers);
    }

    @Override
    public GeoView getGeoView() {
        return _sceneView;
    }

    @Override
    public void addRefenceLayer(Layer l) {
        _sceneView.getScene().getBasemap().getReferenceLayers().add(l);
    }

    @Override
    public void adddRefenceLayers(List<FeatureLayer> ls) {
        _sceneView.getScene().getBasemap().getReferenceLayers().addAll(ls);
    }

    @Override
    public void addOptionLayer(FeatureLayer l) {
        _sceneView.getScene().getOperationalLayers().add(l);
    }

    @Override
    public void addOptionLayers(List<FeatureLayer> ls) {
        _sceneView.getScene().getOperationalLayers().addAll(ls);
    }

    @Override
    public LayerList getBaseLayers() {
        return _sceneView.getScene().getBasemap().getBaseLayers();
    }

    @Override
    public LayerList getOperationalLayers() {
        return _sceneView.getScene().getOperationalLayers();
    }

    @Override
    public ListenableList<GraphicsOverlay> getGraphicsOverlays() {
        return _sceneView.getGraphicsOverlays();
    }

    @Override
    public LayerList getRefrenceLayers() {
        return _sceneView.getScene().getBasemap().getReferenceLayers();
    }
}